package com.hxzy.tms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hxzy.tms.Assert;
import com.hxzy.tms.SessionHolder;
import com.hxzy.tms.dao.TeamDao;
import com.hxzy.tms.dao.UserDao;
import com.hxzy.tms.dao.BaseDao.Params;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TeamService;
/**
 * 团队Service实现类
 * @author team1
 *
 */
public class DefaultTeamService implements TeamService {
	private TeamDao teamDao;
	private UserDao userDao;
	
	public ServiceResult<Team> read(long id) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public ServiceResult<Team> read(String domain) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public ServiceResult<Team> create(String name, String desc, String intro, String subdomain, Date gmtBegin,
			Date gmtEnd) {
		Assert.notBlank(name, "团队名字不能为空");
		Assert.notBlank(desc, "团队描述不能为空");
		Assert.notBlank(intro, "团队介绍不能为空");
		Assert.notEmpty(subdomain, "域名不能为空");
		Assert.notEmpty(gmtBegin, "开始日期不能为空");
		Assert.notEmpty(gmtEnd, "结束日期不能为空");
		Assert.notEmpty(SessionHolder.get(), "必须是登陆用户才能创建团队");
		return saveOrUpdate(null, name, desc, intro, subdomain, gmtBegin, gmtEnd);
	}
	
	private  ServiceResult<Team> saveOrUpdate(Long id,String name, String desc, String intro, String subdomain, Date gmtBegin,
			Date gmtEnd){
		
        if (gmtEnd.getTime() <= gmtBegin.getTime()) {
            return new ServiceResult<Team>(-1, null, "开始时间必须早于预计结束时间");
        }

        if (!(subdomain.charAt(0) >= 'a' && subdomain.charAt(0) <= 'z')) {
            return new ServiceResult<Team>(-2, null, "团队二级域名首字母必须是小写字母 a-z");
        }

        List<Team> teams = new ArrayList<Team>();
        if (id != null) {
            teams = teamDao.read("from Team where name = :name and id != :id", new Params("name", name),
                    new Params("id", id));
        } else {
            teams = teamDao.read("from Team where name = :name", new Params("name", name));
        }
        if (!teams.isEmpty()) {
            return new ServiceResult<Team>(-3, null, "团队名字重复");
        }

        if (id != null) {
            teams = teamDao.read("from Team where subdomain = :subdomain and id != :id",
                    new Params("subdomain", subdomain), new Params("id", id));
        } else {
            teams = teamDao.read("from Team where subdomain = :subdomain", new Params("subdomain", subdomain));
        }
        if (!teams.isEmpty()) {
            return new ServiceResult<Team>(-4, null, "团队个性域名重复");
        }
//		Team team =( teamDao.read(Team.class, id) != null ) ? teamDao.read(Team.class, id) : new Team();
		Team team = new Team();
		team.setName(name);
		team.setDesc(desc);
		team.setIntro(intro);
		team.setStatus(1L);
		team.setSubdomain(subdomain);
		team.setUserCreate(SessionHolder.get());
		team.setGmtModified(0L);
		team.setGmtBegin(gmtBegin.getTime()/1000);
		team.setGmtEnd(gmtEnd.getTime()/1000);
		 if (id == null) {
	            team.setUserCount(0L);
	        }

	        if (id != null) {
	            team.setId(id);
	            teamDao.update(team);
	        } else {
	            teamDao.create(team);
	        }
		return new ServiceResult<Team>(1,team);
		
	}
	
	public ServiceResult<Team> update(long id, String name, String desc, String intro, String subdomain, Date gmtBegin,
			Date gmtEnd) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Team> list(Integer status) {
		// TODO Auto-generated method stub
		return null;
	}

	public int invite(long uid, long teamId, String email) {
		// TODO Auto-generated method stub
		return 0;
	}

	
	public TeamDao getTeamDao() {
		return teamDao;
	}

	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	
    
}